package com.tjetc.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tjetc.constant.CommonConstant;
import com.tjetc.context.BaseContext;
import com.tjetc.dao.BookingDao;
import com.tjetc.dao.GuestDao;
import com.tjetc.dao.RoomDao;
import com.tjetc.dao.impl.BookingDaoImpl;
import com.tjetc.dao.impl.GuestDaoImpl;
import com.tjetc.dao.impl.RoomDaoImpl;
import com.tjetc.exception.MessageException;
import com.tjetc.pojo.dto.BookingDTO;
import com.tjetc.pojo.dto.GuestDTO;
import com.tjetc.pojo.entity.Booking;
import com.tjetc.pojo.entity.Guest;
import com.tjetc.pojo.entity.Room;
import com.tjetc.pojo.vo.GuestIdVO;
import com.tjetc.service.BookingService;
import com.tjetc.utils.DruidUtils;
import com.tjetc.utils.EmailUtil;

import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/5/10 21:07
 * @Description: 预订服务实现类
 */
public class BookingServiceImpl implements BookingService {

    private RoomDao roomDao = new RoomDaoImpl();

    private BookingDao bookingDao = new BookingDaoImpl();

    private GuestDao guestDao = new GuestDaoImpl();

    @Override
    public void save(BookingDTO bookingDTO) {
        commonMethod(bookingDTO);
    }


    @Override
    public void add(BookingDTO bookingDTO) {
        try {
            DruidUtils.start();
            Guest guest = guestDao.getUserId(BaseContext.getCurrentId());
            if (guest==null){
                guestDao.save(BaseContext.getCurrentId());
            }
            bookingDTO.setGuestId(guestDao.getByUserId(BaseContext.getCurrentId()).getGuestId());
            List<Booking> bookingList = bookingDao.getByGuestId(bookingDTO.getGuestId());
            if (bookingList != null && bookingList.size() != 0) {
                throw new MessageException(CommonConstant.ONLY_BOOKING_ONE);
            }
            DruidUtils.commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
        commonMethod(bookingDTO);
    }

    private void commonMethod(BookingDTO bookingDTO) {
        try {
            Booking booking = new Booking();
            Room room = roomDao.getById(bookingDTO.getRoomId());
            if (room == null) {
                throw new MessageException(CommonConstant.NO_FIND_ROOM);
            }
            if (room.getAvailability() > 1) {
                throw new MessageException(CommonConstant.ROOM_IS_BOOKED);
            }
            BeanUtil.copyProperties(bookingDTO, booking);
            booking.setTotalPrice(room.getPrice());
            DruidUtils.start();
            bookingDao.insert(booking);
            roomDao.updateStatusById(booking.getRoomId(), room.getAvailability());
            Guest guest = guestDao.getUserId(BaseContext.getCurrentId());
            if (guest==null){
                guestDao.insert(booking.getGuestId());
            }
            DruidUtils.commit();
            GuestDTO guestDTO = guestDao.getById(booking.getGuestId());
            System.out.println(CommonConstant.ADD_SUCCESS);
            EmailUtil.sendEmail(guestDTO.getEmail(), CommonConstant.HOTEL_NOTICE, CommonConstant.BOOKING_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void delete(Long id) {
        try {
            DruidUtils.start();
            bookingDao.delete(id);
            DruidUtils.commit();
            System.out.println(CommonConstant.DELETE_SUCCESS);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }

    @Override
    public void update(BookingDTO bookingDTO) {
        try {
            Booking booking = new Booking();
            BeanUtil.copyProperties(bookingDTO, booking);
            DruidUtils.start();
            bookingDao.update(booking);
            DruidUtils.commit();
        } catch (Exception e) {
            System.out.println(e.getMessage());
            DruidUtils.rollback();
        }
    }

    @Override
    public void list() throws Exception {
        List<Booking> list = bookingDao.list();
        if (list == null || list.size() == 0) {
            throw new MessageException(CommonConstant.NO_FIND_DATA);
        }
        list.stream().forEach(System.out::println);
    }

}
